package com.example.demo.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.pojo.SysMenu;
import com.example.demo.service.SysMenuService;
import com.example.demo.mapper.SysMenuMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 *
 */
@Service
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu>
    implements SysMenuService{
@Autowired
private SysMenuMapper sysMenuMapper;
  @Override
  public List<SysMenu> list1(Integer userId) {
    List<SysMenu> sysMenus = sysMenuMapper.list1(userId);

    return getChildenNoe(sysMenus);
  }
  public List<SysMenu> getChildenNoe(List<SysMenu> sysMenus ){
    return  sysMenus.stream()
        .filter(imtes ->
            imtes.getParentId()==0L
        ).map(imtes -> {
          imtes.setChildren(getChilden(imtes, sysMenus));
          return imtes;
        }).collect(Collectors.toList());
  }
  public List<SysMenu> getChilden(SysMenu sysMenu,List<SysMenu> sysMenus) {
    return sysMenus.stream()
        .filter(item ->
            sysMenu.getMenuId().equals(item.getParentId())
        ).map(item -> {
          item.setChildren(getChilden(item, sysMenus));
          return item;
        }).collect(Collectors.toList());
  }
}




